package api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

import Util.GsonUtils;
import Util.HttpUtil;

public class textCheck implements APIKey {
	private String replaceBlank(String str) throws UnsupportedEncodingException {

		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
			dest.replace(" ", "");
		}
		return dest;
	}

	/**
	 * 提交文字进行校验
	 * 
	 * @param access_token
	 * @param text
	 * @return
	 * @throws Exception
	 */
	public String postUrl(String access_token, String text) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("content", replaceBlank(text));
		String param = GsonUtils.toJson(map);
		System.out.println(param);
		String resultString = HttpUtil.post(textUrl, access_token,
				"content=" + URLEncoder.encode(replaceBlank(text), "UTF-8"));
		JSONObject jsonObject = new JSONObject(resultString);
		return jsonObject.toString();
	}

}